package com.qianfeng.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.pojo.SysCategory;
import com.qianfeng.smartdevice.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

 /**
 * @time: 2022-03-14 14:19
 */
@RestController
@RequestMapping("/categories")
public class CategoryController {

    private SysCategoryService categoryService;

    @Autowired
    public void setCategoryService(SysCategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/category/{id}")
    public R findById(@PathVariable Long id) {
        SysCategory category = categoryService.findById(id);
        return R.setOK(category);
    }

    @PostMapping("/category")
    public R addSysCategory(@RequestBody SysCategory category) {
        categoryService.addSysCategory(category);
        return R.setOK();
    }

    @GetMapping("/categories")
    public R findAllByPage(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "1")  int pageSize, String categoryName) {
        PageInfo<SysCategory> pageInfo = categoryService.findByPage(pageNum, pageSize, categoryName);
        return R.setOK(pageInfo);
    }

    @PostMapping("/category/edit")
    public R updateCategory(@RequestBody SysCategory category) {
        int count = categoryService.updateCategory(category);
        return R.setOK(count);
    }

    @PostMapping("/category/delete")
    public R deleteByIds(@RequestBody List<Long> ids) {
        int count = categoryService.deleteByIds(ids);
        return R.setOK(count);
    }



}
